package com.hsy.linemoney.modules.my.cash_and_report;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AccountService;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.utils.CommonUtils;
import com.widget.picker.CustomListPickers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 现金流和余额报告
 * Created by Administrator on 2016/7/11.
 */
public class MyCashAndReportActivity extends CommonBaseActivityWithSimpleActionBar {

    private TextView selectAccountSpinner;
    private TextView selectMonthSpinner;
    private ListView listView;

    private List<AccountService.CashflowResult.Transactions> listData;
    private CashAndReportAdapter cashAndReportAdapter;

    public int selectedAccountWhich = -1;
    private int selectedTimeWhich = -1;
    private AccountService.CashAndReportFilterResult mCashAndReportFilterResult;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.title_cash_and_report);
        setContentView(R.layout.activity_my_cash_and_report);

        selectAccountSpinner = (TextView) findViewById(R.id.select_account_spinner);
        selectMonthSpinner = (TextView) findViewById(R.id.select_month_spinner);
        listView = (ListView) findViewById(R.id.listView);

        selectAccountSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] accountNames = mCashAndReportFilterResult.getData().getAccounts();
                showListDialog(accountNames, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selectedAccountWhich = which;
                        selectAccountSpinner.setText(accountNames[which]);
                        loadCashflow();
                    }
                });
            }
        });

        selectMonthSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] periodNames = mCashAndReportFilterResult.getData().getReportPeriodNames();
                showListDialog(periodNames, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selectedTimeWhich = which;
                        selectMonthSpinner.setText(periodNames[which]);
                        loadCashflow();
                    }
                });
            }
        });

        final AccountService service = new AccountService();
        service.getCashAndReportFilter(new Callback<AccountService.CashAndReportFilterResult>() {
            @Override
            public void onSuccess(int what, AccountService.CashAndReportFilterResult result) {
                selectedAccountWhich = 0;
                selectAccountSpinner.setText(result.getData().getAccounts()[0]);

                selectedTimeWhich = 0;
                selectMonthSpinner.setText(result.getData().getReportPeriods().get(0).getLabel());

                MyCashAndReportActivity.this.mCashAndReportFilterResult = result;
                loadCashflow();
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {

            }
        });

    }

    private void loadCashflow() {
        if (checkThrough()) {
            showProgressDialog(this);
            final AccountService service = new AccountService();
            String accountId = mCashAndReportFilterResult.getData().getAccounts()[0];
            String timePeriod = mCashAndReportFilterResult.getData().getReportPeriods().get(0).getValue();
            service.getCashflow(accountId, timePeriod, new Callback<AccountService.CashflowResult>() {
                @Override
                public void onSuccess(int what, AccountService.CashflowResult result) {
                    listData = result.getData().getTransactions();
                    cashAndReportAdapter = new CashAndReportAdapter(MyCashAndReportActivity.this, listData);
                    listView.setAdapter(cashAndReportAdapter);
                    hideProgressDialog();
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {
                    hideProgressDialog();
                }
            });
        }
    }

    private boolean checkThrough() {
        if (null == mCashAndReportFilterResult) {
            return false;
        }
        if (selectedAccountWhich < 0) {
            showToast(R.string.txt_please_select_account);
            return false;
        }
        if (selectedTimeWhich < 0) {
            showToast(R.string.txt_please_select_time_period);
            return false;
        }
        return true;
    }

    private void showListDialog(String[] items, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder dialog = null;
        dialog = new AlertDialog.Builder(this).setItems(items, listener);
        dialog.show();
    }
}
